<template>
	<view class="container">
		<view class="unicard">
			<uni-card class="unic">
				<view style="text-align: center;font-size: 20upx;font-weight: bold;margin: 10upx;">质检系统<text style="color: #007AFF;font-size: 5px;text-align: center;margin-left: 5px;">V{{version}}</text></view>
				<uni-forms ref="form" :modelValue="formData" :rules="rules" label-width="50">
					<uni-forms-item label="账号" username="username"><uni-easyinput v-model="formData.username"></uni-easyinput></uni-forms-item>
					<uni-forms-item label="密码" name="password"><uni-easyinput v-model="formData.password" type="password"></uni-easyinput></uni-forms-item>
					<button type="primary" @click="formSubmit">登录</button>
				</uni-forms>
			</uni-card>
		</view>
		<yomol-prompt title="修改名称" :maxlength="500" defaultValue="yomol" ref="yomolPrompt" @onConfirm="onPromptConfirm"></yomol-prompt>
		<yomol-upgrade :type="upgradeType" :url="upgradeUrl" title="发现新版本" :content="upgradeContent" ref="yomolUpgrade"></yomol-upgrade>
	</view>
</template>

<script>

export default {
	
	data() {
		return {
			version:43,
			upgradeType: 'pkg', //pkg 整包 wgt 升级包
			upgradeContent: '', //更新内容
			upgradeUrl: '', //更新地址
			serverUrl: '',
			formData: {
				username: '',
				password: ''
			},
			rules: {
				// 对name字段进行必填验证
				username: {
					rules: [
						{
							required: true,
							errorMessage: '请输入姓名'
						},
						{
							minLength: 2,
							maxLength: 10,
							errorMessage: '姓名长度在 {minLength} 到 {maxLength} 个字符'
						}
					]
				},
				// 对password字段进行必填验证
				password: {
					rules: [
						{
							required: true,
							errorMessage: '请输入密码'
						}
					]
				}
			}
		};
	},
	onLoad() {
		this.checkVersionClick();
		this.formData.username = uni.getStorageSync('username');
		this.formData.password = uni.getStorageSync('password');
	},
	methods: {
		onPromptConfirm(e) {
			this.value = e;
			console.log(this.value);
		},
		checkVersionClick() {
			// 检测升级
			plus.runtime.getProperty(plus.runtime.appid, widgetInfo => {
				var platform = uni.getSystemInfoSync().appName;
				//根据当前平台去后台检测版本号
				//这是我的逻辑，自己自定义
				//this.$refs.yomolUpgrade.show();
				
				this.$http.get(
					"version/getNext",
					{
						id:this.version
					}
				).then(res=>{
					if(200 == res.data.code){
						const data = res.data.data.data
						if(data.path){
							this.upgradeContent = data.remark||''
							this.upgradeUrl = data.path
							this.$refs.yomolUpgrade.show();
						}
					}
				})
				
				// uni.request({
				// 	url: this.serverUrl + '/version/check',
				// 	data: {
				// 		ver: widgetInfo.version
				// 	},
				// 	success: res => {
				// 		console.log(res);
				// 		if (res.data[0]) {
				// 			if (res.data[0].Version != widgetInfo.version) {
				// 				console.log(res.data[0].upgradeContent);
				// 				console.log(res.data[0].upgradeUrl);
				// 				this.upgradeContent = res.data[0].upgradeContent;
				// 				this.upgradeUrl = res.data[0].upgradeUrl;
				// 				this.$refs.yomolUpgrade.show();
				// 			}
				// 		}
				// 	},
				// 	fail: error => {
				// 		console.log(error);
				// 	}
				// });
			});
		},
		formSubmit() {
			uni.setStorageSync("username",this.formData.username)
			uni.setStorageSync("password",this.formData.password)
			this.$refs.form
				.validate()
				.then(res => {
					this.$http.post('baseUser/login', this.formData, { header: { 'Content-Type': 'application/x-www-form-urlencoded' } }).then(res => {
						if (res.data.code === 200) {
							uni.setStorage({
								key: 'token',
								data: res.data.data.token
							});
							uni.showToast({
								title: '登录成功!',
								duration: 1000
							});
							setTimeout(() => {
								uni.redirectTo({
									url: '/pages/menu/index'
								});
							}, 1000);
						} else {
							uni.showToast({
								icon: 'error',
								title: res.data.message
							});
						}
					});
				})
				.catch(e => {
					console.log(e);
				});
		}
	}
};
</script>

<style>
.container {
	background-image: url('/static/login.jpeg');
	font-size: 14px;
	width: 100vw;
	height: 100vh;
	display: flex;
	justify-content: flex-end;
	align-items: center;
}
.unicard {
	margin-top: 60upx;
	margin-right: 10upx;
}
.unic {
	padding: 0 20upx 20upx 20upx;
}
</style>
